<?php 
$data = $this->element('admin/treeMenuData');
$trees = json_decode($data,true);
function parseLink($Menu){
	if ($Menu['Menu']['bind_event']==0) {
		return 'javascript:;';
	}
	$linkAction = split('_',$Menu['Menu']['action']);
	if(isset($linkAction[0])&&$linkAction[0]=='admin'){
		$admin = true;
		$action = $linkAction[1];
	}else{
		$admin = false;
		$action ='';
	}
	return array(
		'admin'=>$admin,
		'plugin'=>$Menu['Menu']['plugin']=='false'?false:$Menu['Menu']['plugin'],
		'controller'=>$Menu['Menu']['controller'],
		'action'=>$action
	);
}
function locateMenu($menu,$arr = false){
	if($arr){
		if($menu['Menu']['id']==$arr['Menu']['id']
			||$menu['Menu']['id']==$arr['Menu']['parent_id']){
				return ' class="current" ';
			}
	}
	return '';
}
?>

<div id="sidebar">
	<ul>
	<?php foreach ($trees as $tree) ://pr($tree)?>
			<li <?php echo locateMenu($tree,$locate)?>>
				<?php 
					$icon = ($tree['Menu']['icon']==''?'default':$tree['Menu']['icon']).'.png';
					echo $this->Html->link(
        				$this->Html->image('bootstrap/'.$icon).' '.$tree['Menu']['title'],
		        		parseLink($tree),array('escape'=>false));
		        	if(!empty($tree['SonMenu'])){
		        		echo '<ul>';	
		        		foreach ($tree['SonMenu'] as $stree) :
		        			echo '<li'.locateMenu($stree,$locate).'>';
		        			echo $this->Html->link($stree['Menu']['title'],parseLink($stree));
		        			if(!empty($stree['SonMenu'])){
		        				echo '<ul>';	
				        		foreach ($stree['SonMenu'] as $ttree) :
				        			echo '<li'.locateMenu($ttree,$locate).'>';
				        			echo $this->Html->link($ttree['Menu']['title'],parseLink($ttree));
				        			echo '</li>';
								endforeach;
				        		echo '</ul>';
		        			}
		        			echo '</li>';
						endforeach;
		        		echo '</ul>';
		        	}
        		?>
        		
			</li>
	<?php endforeach;?>
	</ul>
	<a href="#collapse" id="menucollapse">&#9664; 折叠菜单</a>
</div>